package com.travelsky.airchina.boarding.web.exception;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import com.travelsky.airchina.boarding.util.StringPrintWriter;


public class MyHandlerExceptionResolver implements HandlerExceptionResolver {
	private static final Logger logger = Logger.getLogger(MyHandlerExceptionResolver.class);
	private String showSystemError;//显示系统后台管理的错误异常页面
	private String showFrontError;//显示系统前台的错误异常页面
	public String getShowSystemError() {
		return showSystemError;
	}
	public void setShowSystemError(String showSystemError) {
		this.showSystemError = showSystemError;
	}
	public String getShowFrontError() {
		return showFrontError;
	}
	public void setShowFrontError(String showFrontError) {
		this.showFrontError = showFrontError;
	}
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		String className = handler.getClass().getName();
        logger.error("Catch Exception: ",ex);//把漏网的异常信息记入日志
        Map<String,Object> map = new HashMap<String,Object>();
        StringPrintWriter strintPrintWriter = new StringPrintWriter();
        ex.printStackTrace(strintPrintWriter);
        map.put("errorMsg", strintPrintWriter.getString());//将错误信息传递给view

		if(className.indexOf("com.travelsky.airchina.boarding.web.admin")!=-1){
	        return new ModelAndView(showSystemError,map);
		}else{
	        return new ModelAndView(showFrontError,map);
		}
		
	}


}
